package ar.edu.sovietrussia.customcomponents.personconfig;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.user.PersonService;
import systemconfig.SystemConfiguration;
import users.Person;
import users.Role;
import users.UserAccount;
import ar.edu.sovietrussia.customcomponents.util.InwardReplaceablePanel;

/**
 * Component for new users creation
 */

@AuthorizeInstantiation("ADMIN")
public class CreateNewUserComponent extends InwardReplaceablePanel {

    private static final long serialVersionUID = 7827634167593023084L;

    @SpringBean(name = "service.person")
    private PersonService personService;

    private TextField<String> name;

    private TextField<String> surname;

    private TextField<String> mail;

    private TextField<String> userID;

    private TextField<String> password;

    private DropDownChoice<String> roles;

    private String selectedRole;

    public DropDownChoice<String> getRoles() {
        return roles;
    }

    public void setRoles(final DropDownChoice<String> roles) {
        this.roles = roles;
    }

    public String getSelectedRole() {
        return selectedRole;
    }

    public void setSelectedRole(final String selectedRole) {
        this.selectedRole = selectedRole;
    }

    public void setName(final TextField<String> name) {
        this.name = name;
    }

    public void setSurname(final TextField<String> surname) {
        this.surname = surname;
    }

    public void setMail(final TextField<String> mail) {
        this.mail = mail;
    }

    public void setUserID(final TextField<String> userID) {
        this.userID = userID;
    }

    public void setPassword(final TextField<String> password) {
        this.password = password;
    }

    public CreateNewUserComponent(final String id, final Class<?> clazz) {
        super(id);

        Form<String> form = new Form<String>("personalConfigForm");

        name = new TextField<String>("name", new Model<String>(""));
        surname = new TextField<String>("surname", new Model<String>(""));
        mail = new TextField<String>("mail", new Model<String>(""));
        userID = new TextField<String>("userID", new Model<String>(""));
        password = new TextField<String>("password", new Model<String>(""));

        form.add(name);
        form.add(surname);
        form.add(mail);
        form.add(userID);
        form.add(password);

        roles = new DropDownChoice<String>("roles", new PropertyModel<String>(this, "selectedRole"),
                SystemConfiguration.getInstance().getRolesInApplicationAsStrings());
        form.add(roles);

        // Accept Button
        form.add(new Button("accept_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {

                CreateNewUserComponent.this.storeNewPerson();
                CreateNewUserComponent.this.goBack(clazz);
            }

        });

        // Go Back Button
        form.add(new Button("back_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                CreateNewUserComponent.this.goBack(clazz);
            }
        });

        this.add(form);
    }

    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    public PersonService getPersonService() {
        return personService;
    }

    // -------------------------------------------------------_//

    private void storeNewPerson() {
        final Person person = new Person(getName(), getSurname(), new UserAccount(getUserID(), getPassword(),
                getRoleInDropDownBox()), getMail());
        personService.addPerson(person);
    }

    // -------------------------------------------------------_//

    protected String getName() {
        return name.getDefaultModelObjectAsString();
    }

    protected String getSurname() {
        return surname.getDefaultModelObjectAsString();
    }

    protected String getMail() {
        return mail.getDefaultModelObjectAsString();
    }

    protected String getUserID() {
        return userID.getDefaultModelObjectAsString();
    }

    protected String getPassword() {
        return password.getDefaultModelObjectAsString();
    }

    public Role getRoleInDropDownBox() {
        return SystemConfiguration.getInstance().getRoleByName(selectedRole);
    }
}
